package com.ruoyi.web.controller.lims;

import com.alibaba.fastjson2.JSONArray;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.lims.domain.LimBuildStorage;
import com.ruoyi.lims.service.LimBuildStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * lhp
 * 2023/7/27
 */
@RestController
@RequestMapping("/buildStorage")
public class LimBuildStorageController extends BaseController {
    @Autowired
    private LimBuildStorageService limBuildStorageService;
    @Log(title = "查询建库信息", businessType = BusinessType.OTHER)
    @PreAuthorize("@ss.hasPermi('lims:storage:query')")
    @RequestMapping("/list")
    public TableDataInfo list(LimBuildStorage storage){
        startPage();
        List<LimBuildStorage> list = limBuildStorageService.getList(storage);
        return getDataTable(list);
    }
    @Log(title = "删除建库信息", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('lims:storage:del')")
    @RequestMapping("/delList")
    public AjaxResult delList(@RequestBody List<String> ids){
        AjaxResult ajaxResult = limBuildStorageService.delList(ids);
        return ajaxResult;
    }
    @Log(title = "导出表格", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('lims:storage:export')")
    @RequestMapping("/export")
    public void export(HttpServletResponse response, @RequestParam("list") String list,@RequestParam("batchId") String batchId) throws IOException {
        List<String> ids = JSONArray.parseArray(list, String.class);
        List<LimBuildStorage> rows = limBuildStorageService.export(ids,batchId);
        ExcelUtil<LimBuildStorage> util = new ExcelUtil<LimBuildStorage>(LimBuildStorage.class);
        util.setLock(true);
        util.exportExcel(response, rows, "建库信息数据");
    }
    @Log(title = "导入表格", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('lims:storage:export')")
    @RequestMapping("/importStorage")
    public AjaxResult importData(MultipartFile file, boolean updateSupport,String batchId) throws Exception
    {
        ExcelUtil<LimBuildStorage> util = new ExcelUtil<LimBuildStorage>(LimBuildStorage.class);
        List<LimBuildStorage> storageList = util.importExcel(file.getInputStream());
        String message = limBuildStorageService.importExcel(storageList, updateSupport, getUsername(),batchId);
        return success(message);
    }
    @Log(title = "修改建库信息", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('lims:storage:edit')")
    @PostMapping("/updataStorageList")
    public AjaxResult updataStorageList(@RequestBody Map data )
    {
        AjaxResult ajaxResult =limBuildStorageService.updataStorageList(data);
        return ajaxResult;
    }
    @Log(title = "建库信息-上传附件", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('lims:storage:export')")
    @PostMapping("/importAttachment")
    public AjaxResult importAttachment(MultipartFile file, HttpServletRequest request)
    {
        return success( limBuildStorageService.importAttachment(file, request.getParameter("ids")));
    }
    @Log(title = "周期计算", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('lims:storage:cycle')")
    @GetMapping("/getCycle/{batchId}")
    public AjaxResult getCycle(@PathVariable String batchId)
    {
        return success( limBuildStorageService.getCycle(batchId));
    }

}
